/*!
 * @file opp_init.c
 * @brief opp initialization
 * @par Copyright (C) 2008 ALPS ELECTRIC CO., LTD
 */


/****************************************************************************
 * Includes
 ****************************************************************************/
#include "../alps_app/private.h"



/*!
 * @brief Initialise Object push profile.
 * 
 * updated opp status
 * 
 * This function will be called by appInit fucntion during system initialising process.
 */
void oppInitialize(void)
{
	oppUpdateState(oppNone);
	app.opp.opp_status = 0;
	app.opp.opp_ifheader = 0;
	app.opp.opp_fc = oppFcGo;//0-go, non-0 stop data transfer.
	app.opp.data_continue = 0;
	app.opp.opp_limit_con = 0;
}

/*!
 * @brief Startup OPP service.
 * 
 * Send message TCU_OPP_SVR_SETUP_REQ to stack to start opp service.
 * 
 * after send startup command, the oppProfileHandler will receive an TCU_OPP_SVR_SETUP_RESP message.
 */
int oppStartUp(void)
{
	stack_msg msg;
	
	createStackMsg(TSB_OPP, TCU_OPP_SVR_SETUP_REQ, &msg);
	return sendStackMessage(&msg);
}

/*!
 * @brief Stop OPP service.
 * 
 * to send message TCU_OPP_SVR_SHUTDOWN_REQ to stack to stop opp service
 * 
 * After send shutdown command, the oppProfileHandler will receive an TCU_OPP_SVR_SHUTDOWN_RESP message.
 * 
 */
int oppStop(void)
{
	stack_msg msg;
	
	createStackMsg(TSB_OPP, TCU_OPP_SVR_SHUTDOWN_REQ, &msg);
	
	return sendStackMessage(&msg);
}

/*!
 * @brief .
 * 
 * 
 * 
 *.
 * 
 */
void eventOPPStartup(param_data* data)
{
	uint8 status;
	
	paramGet8bitVal(data, &status);//get statua
	
	if(C2_OK == status)
	{
		oppUpdateState(oppReady);
		
		updateRuningService(srv_reg_add, SERVICE_OPP);//add opp to runing service
		
		if(sysInit == app.system_state || HOST_GEN_SERVICE_CONFIG_REQ == app.host_req)
		{
			locRegisterService();
		}
		else
		{
			//should never be here, execpt using TSB message directely.
		}
	}
	else
	{
		if(HOST_GEN_SERVICE_CONFIG_REQ == app.host_req)//(un)register service
		{
			genericHostMsgTwoExt(HOST_GEN_SERVICE_CONFIG_REQ, EVTRES_OPERATION_FAIL, app.runing_service);
		}
		else
		{
			evtGenAppInitStatusInd(EVTINT_FAIL);
		}
	}
}



void eventOPPShutdown(param_data* data)
{
	uint8 status;
	paramGet8bitVal(data, &status);//get status
	if(C2_OK != status)
	{
		if(HOST_GEN_SERVICE_CONFIG_REQ == app.host_req)//(un)register service
		{
			genericHostMsgTwoExt(HOST_GEN_SERVICE_CONFIG_REQ, EVTRES_OPERATION_FAIL, app.runing_service);
		}
		else
		{
			//should never be here, execpt using TSB message directely.
		}
		return;
	}
	
	oppUpdateState(oppNone);

	updateRuningService(srv_reg_remove, SERVICE_OPP);//remove opp from runing service
			
	if(HOST_GEN_SERVICE_CONFIG_REQ == app.host_req)//(un)register service
	{
		locRegisterService();
	}
}

